package com.tcl.logoidentify.commons;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServletUtils {
    /**
     * 获取HttpServletRequest中包含的ipv4地址,获取不到返回127.0.0.1
     * 
     * @param request
     * @return
     */
    protected static Logger logger = LoggerFactory.getLogger(ServletUtils.class);

    public static String getIpByRequest(HttpServletRequest request) {
        String xForwardedFor = request.getHeader("x-forwarded-for");
        String ip = null;
        if (xForwardedFor != null) {
            ip = StringUtils.split(xForwardedFor, ",")[0];
            if (isIpValid(ip)) {
                return ip;
            }
        }
        ip = request.getHeader("Proxy-Client-IP");
        if (isIpValid(ip)) {
            return ip;
        }
        ip = request.getHeader("WL-Proxy-Client-IP");
        if (isIpValid(ip)) {
            return ip;
        }
        ip = request.getRemoteAddr();
        if (isIpValid(ip)) {
            return ip;
        }
        return "127.0.0.1";
    }

    private static boolean isIpValid(String ip) {
        if (ip == null || ip.length() == 0 || "unkown".equalsIgnoreCase(ip) || ip.split("\\.").length != 4) {
            return false;
        }
        return true;
    }
}
